import { getListData } from '../../api/order/index'
let order = {
    namespaced: true,
    state: {
        list1: JSON.parse(localStorage.getItem('orderlist')) || [],
        index: 0
    },
    mutations: {
        Setdata(state, plyload) {
            state[plyload.key] = plyload.value
        }
    },
    actions: {
        async Getorderlist({ commit }) {
            let res = await getListData()
            commit('Setdata', {
                key: 'list1',
                value: res.data.list
            })
        },
        SetListAdd({ commit, state }, id) {
            state.list1.forEach((item) => {
                item.children.forEach((v, i) => {
                    if (v.id === id) {
                        v.num++
                    }
                })
            })
            commit('Setdata', {
                key: 'list1',
                value: state.list1
            })
        },
        SetListRelief({ commit, state }, id) {
            state.list1.forEach((item) => {
                item.children.forEach((v, i) => {
                    if (v.id === id) {
                        if (v.num > 0) {
                            v.num--
                        }
                    }
                })
            })
            commit('Setdata', {
                key: 'list1',
                value: state.list1
            })
        },
    },
    getters: {
        GetSum(state) {
            let he = 0
            state.list1.forEach(item => {
                item.children.forEach(v => {
                    he += (v.price * v.num)
                })
            })
            return he
        }
    }

}

export default order